﻿Imports System.ComponentModel
Imports Microsoft.Win32

Friend Class OleDBSqlConnectionProperties
    Inherits OleDBSpecializedConnectionProperties
    ' Methods
    Public Sub New()
        MyBase.New("SQLOLEDB")
        Me.LocalReset()
    End Sub

    Protected Overrides Function GetProperties(ByVal attributes As Attribute()) As PropertyDescriptorCollection
        Dim properties As PropertyDescriptorCollection = MyBase.GetProperties(attributes)
        If OleDBSqlConnectionProperties.SqlNativeClientRegistered Then
            Dim descriptor As DynamicPropertyDescriptor = TryCast(properties.Find("Provider", True), DynamicPropertyDescriptor)
            If (descriptor Is Nothing) Then
                Return properties
            End If
            If Not MyBase.DisableProviderSelection Then
                descriptor.SetIsReadOnly(False)
            End If
            descriptor.SetConverterType(GetType(SqlProviderConverter))
        End If
        Return properties
    End Function

    Private Sub LocalReset()
        Me.Item("Integrated Security") = "SSPI"
    End Sub

    Public Overrides Sub Reset()
        MyBase.Reset()
        Me.LocalReset()
    End Sub


    ' Properties
    Public Overrides ReadOnly Property IsComplete As Boolean
        Get
            If Not MyBase.IsComplete Then
                Return False
            End If
            If (Not TypeOf MyBase.ConnectionStringBuilder.Item("Data Source") Is String OrElse (TryCast(MyBase.ConnectionStringBuilder.Item("Data Source"), String).Length = 0)) Then
                Return False
            End If
            If (((MyBase.ConnectionStringBuilder.Item("Integrated Security") Is Nothing) OrElse Not MyBase.ConnectionStringBuilder.Item("Integrated Security").ToString.Equals("SSPI", StringComparison.OrdinalIgnoreCase)) AndAlso (Not TypeOf MyBase.ConnectionStringBuilder.Item("User ID") Is String OrElse (TryCast(MyBase.ConnectionStringBuilder.Item("User ID"), String).Length = 0))) Then
                Return False
            End If
            Return True
        End Get
    End Property

    Public Shared ReadOnly Property SqlNativeClientProviders As List(Of String)
        Get
            If (OleDBSqlConnectionProperties._sqlNativeClientProviders Is Nothing) Then
                OleDBSqlConnectionProperties._sqlNativeClientProviders = New List(Of String)
                Dim str As String
                For Each str In OleDBConnectionProperties.GetRegisteredProviders
                    If str.StartsWith("SQLNCLI") Then
                        Dim index As Integer = str.IndexOf(".")
                        If (index > 0) Then
                            OleDBSqlConnectionProperties._sqlNativeClientProviders.Add(str.Substring(0, index).ToUpperInvariant)
                        End If
                    End If
                Next
                OleDBSqlConnectionProperties._sqlNativeClientProviders.Sort()
            End If
            Return OleDBSqlConnectionProperties._sqlNativeClientProviders
        End Get
    End Property

    Private Shared ReadOnly Property SqlNativeClientRegistered As Boolean
        Get
            If Not OleDBSqlConnectionProperties._gotSqlNativeClientRegistered Then
                Dim key As RegistryKey = Nothing
                Try
                    OleDBSqlConnectionProperties._sqlNativeClientRegistered = (OleDBSqlConnectionProperties.SqlNativeClientProviders.Count > 0)
                Finally
                    If (Not key Is Nothing) Then
                        key.Close()
                    End If
                End Try
                OleDBSqlConnectionProperties._gotSqlNativeClientRegistered = True
            End If
            Return OleDBSqlConnectionProperties._sqlNativeClientRegistered
        End Get
    End Property


    ' Fields
    Private Shared _gotSqlNativeClientRegistered As Boolean
    Private Shared _sqlNativeClientProviders As List(Of String) = Nothing
    Private Shared _sqlNativeClientRegistered As Boolean

    ' Nested Types
    Private Class SqlProviderConverter
        Inherits StringConverter
        ' Methods
        Public Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As StandardValuesCollection
            Dim values As New List(Of String)
            values.Add("SQLOLEDB")
            Dim str As String
            For Each str In OleDBSqlConnectionProperties.SqlNativeClientProviders
                values.Add(str)
            Next
            Return New StandardValuesCollection(values)
        End Function

        Public Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function

        Public Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function

    End Class
End Class


